#include "win5_advertising.h"

pthread_t thread;
lv_obj_t *win5;
lv_obj_t *win5_http_label;

static void Shopping_cb(lv_event_t *e) // 按钮事件函数
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t *obj = lv_event_get_target(e);
    if (code == LV_EVENT_CLICKED)
    {
        //结束线程
        //pthread_cancel(thread);
        lv_obj_del(win5);
        printf("返回购物界面\n");
        win3_sell();
        
    }
}

void win5_advertising(void)
{
    win5 = lv_obj_create(lv_scr_act());
    lv_obj_set_size(win5, 800, 480);
    lv_obj_set_style_pad_all(win5,0,0);
    lv_obj_clear_flag(win5, LV_OBJ_FLAG_SCROLLABLE);
    
    lv_font_t *font1 = lv_freetype_font_create("/root/simkai.ttf", // 字体文件路径
                                              LV_FREETYPE_FONT_RENDER_MODE_BITMAP,
                                              50,
                                              LV_FREETYPE_FONT_STYLE_NORMAL);

    if (!font1)
    {
        LV_LOG_ERROR("freetype font create failed.");
        return;
    }

    /*Create style with the new font*/
    static lv_style_t style1;
    lv_style_init(&style1);
    lv_style_set_text_font(&style1, font1);
    lv_style_set_text_align(&style1, LV_TEXT_ALIGN_CENTER);
    //通知
    lv_obj_t *win5_Notification = lv_obj_create(win5);
    lv_obj_set_size(win5_Notification, 595, 95);
    lv_obj_set_style_bg_color(win5_Notification,lv_color_hex(0xFFFFFF),0);
    lv_obj_set_align(win5_Notification, LV_ALIGN_TOP_LEFT);
    lv_obj_t *win5__label = lv_label_create(win5_Notification);
    lv_obj_add_style(win5__label, &style1, 0);
    lv_label_set_text_fmt(win5__label, "欢迎使用嵌启售货");
    lv_obj_set_align(win5__label, LV_ALIGN_CENTER);

    lv_font_t *font = lv_freetype_font_create("/root/simkai.ttf", // 字体文件路径
                                              LV_FREETYPE_FONT_RENDER_MODE_BITMAP,
                                              24,
                                              LV_FREETYPE_FONT_STYLE_NORMAL);

    if (!font)
    {
        LV_LOG_ERROR("freetype font create failed.");
        return;
    }

    /*Create style with the new font*/
    static lv_style_t style;
    lv_style_init(&style);
    lv_style_set_text_font(&style, font);
    lv_style_set_text_align(&style, LV_TEXT_ALIGN_CENTER);
    //http获取时间天气
    lv_obj_t *win5_http = lv_obj_create(win5);
    lv_obj_set_size(win5_http, 200, 200);
    lv_obj_set_style_pad_all(win5_http,0,0);
    lv_obj_clear_flag(win5_http, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_set_style_bg_color(win5_http,lv_color_hex(0xFFFFFF),0);
    lv_obj_set_align(win5_http, LV_ALIGN_TOP_RIGHT);
    win5_http_label = lv_label_create(win5_http);
    lv_obj_add_style(win5_http_label, &style, 0);
    //lv_label_set_text_fmt(win5_http_label, "%s %s\n %s",city_addr,date_h,date_day);
    lv_obj_set_align(win5_http_label, LV_ALIGN_TOP_LEFT);

    lv_obj_t *weather_img;
    weather_img = lv_image_create(win5_http);
    lv_image_set_src(weather_img, "A:./Data/weather/晴天.png");
    lv_obj_align(weather_img, LV_ALIGN_BOTTOM_RIGHT,-20,0);

    //图片视屏
    lv_obj_t *win5_img = lv_obj_create(win5);
    lv_obj_set_size(win5_img, 595, 380);
    lv_obj_set_style_pad_all(win5_img,0,0);
    lv_obj_clear_flag(win5_img, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_set_style_bg_color(win5_img,lv_color_hex(0x3c3c3c),0);
    lv_obj_set_align(win5_img, LV_ALIGN_BOTTOM_LEFT);
    lv_obj_add_event_cb(win5_img, Shopping_cb, LV_EVENT_CLICKED, NULL);


    lv_obj_t *win5_logo = lv_obj_create(win5);
    lv_obj_set_size(win5_logo, 200, 275);
    lv_obj_set_style_pad_all(win5_logo,0,0);
    lv_obj_clear_flag(win5_logo, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_set_align(win5_logo, LV_ALIGN_BOTTOM_RIGHT);
    lv_obj_t* logo_img = lv_img_create(win5_logo);
    LV_IMG_DECLARE(logo);
    lv_img_set_src(logo_img, &logo);
    lv_obj_set_align(logo_img, LV_ALIGN_CENTER);
    lv_img_set_zoom(logo_img, 160);
    pthread_create(&thread,NULL, HTTP_get, NULL);  //设置分离属性启动线程

    lv_obj_t* img = lv_img_create(win5_img);
    LV_IMG_DECLARE(guanggao);
    lv_img_set_src(img, &guanggao);
    lv_img_set_zoom(img, 180);
    lv_obj_set_align(img, LV_ALIGN_CENTER);
}


